package Dao;

import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.List;

import javax.swing.JOptionPane;

import entity.User;
import util.Dbconnect;

public class UserDaoimpl implements UserDao{

	/**
	* Title: findByid
	* Description: 用email 查询用户
	* @author: 张文涛
	* @date: 2020年12月26日,上午10:40:32
	* @param email
	* @return
	* @throws Exception
	 */
	@Override
	public User findByid(String email) throws Exception {
		
		Dbconnect.init();
		String sql="select * from user where email='"+email+"'";
		User us = Dbconnect.queryone(sql, User.class);
		Dbconnect.close();
		return us;
	}
	
	/**
	* Title: add
	* Description: 注册
	* @author: 张文涛
	* @date: 2020年12月26日,上午10:40:14
	* @param u
	* @return
	* @throws Exception
	 */
	@Override
	public Integer add(User u) throws Exception {
		// TODO Auto-generated method stub
		Dbconnect.init();
		String sql="select * from user where email='"+u.getEmail()+"'";
		User user = Dbconnect.queryone(sql, User.class);
		if(user != null) {
			return -1; 
		}else {
			sql="insert into user(email,sex,password,typeid,name) values('"+u.getEmail()+"','"+u.getSex()+"','"+u.getPassword()+"','"+u.getTypeid()+"','"+u.getName()+"')";
			Dbconnect.update(sql);
		}
		Dbconnect.close();
		return 0;
	}
	

	/**
	* Title: update
	* Description: 更新用户信息
	* @author: 张文涛
	* @date: 2020年12月26日,上午10:42:23
	* @param u
	* @return
	* @throws Exception
	 */
	@Override
	public boolean update(User u) throws Exception {
		// TODO Auto-generated method stub
		Dbconnect.init();
		String sql="update user set email='"+u.getEmail()+"',name = '"+u.getName()+"',sex='"+u.getSex()+"',password='"+u.getPassword()+"',typeid="+u.getTypeid()+" where email ='"+u.getEmail()+"'";
		int update = Dbconnect.update(sql);
		Dbconnect.close();
		return update>0;
	}

	
    /**
    * Title: getUser
    * Description: 获取单个用户
    * @author: 张文涛
    * @date: 2020年12月26日,上午10:37:54
    * @param email
    * @param password
    * @param typeid
    * @return User
    * @throws Exception
     */
	@Override
	public User getUser(String email, String password, Integer typeid) throws Exception {
		// TODO Auto-generated method stub
		Dbconnect.init();
		String sql="select * from user where email='"+email+"' and password = '"+password+"' and typeid = "+typeid;
		User user = Dbconnect.queryone(sql, User.class);
		Dbconnect.close();
		return user;
	}
	
	/**
	 * 
	* Title: getUserList
	* Description: 获取用户列表
	* @author: 张文涛
	* @date: 2020年12月26日,上午10:36:38
	* @return
	* @throws Exception
	 */
	@Override
	public List<User> getUserList() throws Exception {
		// TODO Auto-generated method stub
		Dbconnect.init();
		String sql="select * from user";
		List<User> query = Dbconnect.query(sql, User.class);
		Dbconnect.close();
		return query;
	}
	
	@Override
	public void remove(String userid) throws SQLException{
		
	}
	
	public static void main(String[] args) {
		
	}

}
